<template>
  <div class="doctorinfo" style="margin: 20px 0; " >
    <el-form :model="doctorinfo" :rules="rules" ref="doctorinfo" label-position="right" label-width="100px">
      <el-form-item label="医生姓名"  prop="doctorName" >
        <el-input  v-model="doctorinfo.doctorName"></el-input>
      </el-form-item>
      <el-form-item label="工号（账号）"  >
        <el-input  v-model="doctorinfo.doctorId" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="密码" >
        <el-input :disabled="true" placeholder="******"></el-input >
      </el-form-item>
      <el-form-item label="邮箱" prop="doctorEmail">
        <el-input  v-model="doctorinfo.doctorEmail"></el-input>
      </el-form-item>
      <el-form-item label="电话号码" prop="doctorPhoneNumber">
        <el-input  v-model="doctorinfo.doctorPhoneNumber"></el-input>
      </el-form-item>
      <el-form-item label="科号" prop="departmentId" >
        <el-input :disabled="true" v-model="departmentName" ></el-input>
      </el-form-item>
      <el-form-item label="室号" prop="roomId">
        <el-input :disabled="true" v-model="doctorinfo.roomId"></el-input>
      </el-form-item>
      <el-form-item label="管理员权限" prop="doctorIsAdmin">
        <el-input :disabled="true" v-model="doctorinfo.doctorIsAdmin"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="upinfo('doctorinfo')">保存</el-button>
        <el-button type="primary" @click="changepassword">忘记密码</el-button>
      </el-form-item>
    </el-form>
    <el-dialog title="确认修改" :visible.sync="dialogFormVisible">
      <el-form label-width="100px">
        <el-form-item label="原密码">
          <el-input style="width: 200px" v-model="oldpassword"></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input style="width: 200px" v-model="newpassword"></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input style="width: 200px" v-model="newpasswordag"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "doctorinfo",
  data(){
    return {
      plate:"个人",
      func:"信息管理",
      count:0,
      dialogFormVisible:false,
      oldpassword:"1",
      newpassword:"1",
      newpasswordag:"1",
      password:'******',
      departmentName:"",
      doctorinfo: {
        // doctorId:0,
        // doctorName:"",
        // doctorEmail: "11",
        // doctorPhoneNumber: "33",
        // departmentId:"44",
        // roomId: "55",
        // doctorIsAdmin:"66",
        // doctorPassword:xxx
      },
      rules:{
        doctorName:[
          {required:true,message:"医生不可为空",trigger:"blur"},
          {min:2,max:10,message: "长度在 2 到 10 之间" ,trigger:"blur"}
        ],
        doctorEmail: [
          {required:true,message:"邮箱不可为空",trigger:"blur"},
          {min:0,max:999,message: "长度在 0 到 999 之间",trigger:"blur"}
        ],
        doctorPhoneNumber: [
          {required:true,message:"电话号码不可为空",trigger:"blur"},
          {min:11,max:11,message: "长度为11位",trigger:"blur"}
        ]
      }
    }
  },
  created() {
    this.$emit("generateTab",this.plate,this.func)
    this.load()
  },
  methods: {
    load() {
      this.doctorinfo.doctorId = JSON.parse(localStorage.getItem("doctor")).doctorId
      this.request.get("doctor/getDoctorInfo?doctorId="+this.doctorinfo.doctorId).then(res => {
        if(res.code=="200"){
          let tab = res.data.doctor
          this.departmentName = res.data.departmentName
          this.doctorinfo = tab
          // this.doctorinfo.doctorId = tab.doctorId
          // this.doctorinfo.doctorName = tab.doctorName
          // this.doctorinfo.doctorEmail = tab.doctorEmail
          // this.doctorinfo.doctorPhoneNumber =tab.doctorPhoneNumber
          // this.doctorinfo.departmentId = tab.departmentId
          // this.doctorinfo.roomId =tab.roomId
          // this.doctorinfo.doctorIsAdmin = tab.doctorIsAdmin
          this.password="******"
          console.log(tab)
        }
        else{
          this.$message.error("数据失败！");
        }
      })
    },

    //点击确认保存或更新用户信息
    upinfo(formName){
      this.dialogFormVisible=false;
      this.$refs[formName].validate((valid)=>{
        if(valid){
          console.log('success');
          this.request.post("doctor/save",this.doctorinfo).then(res=>{
            console.log(res)
            if(res.code=="200"){
              this.$message.success("保存成功！请重新登陆！")
              this.$router.push("/")
            }else{
              this.$message.error("保存失败！");
            }
          })
        }
        else{
          console.log('error');
        }
      })
    },
    confirm(){
      if(this.newpassword.length<1 || this.newpassword.length>50){
        this.$message.error("密码长度应在 1 到 50 之间");
      }
      else if(this.newpassword!=this.newpasswordag){
        this.$message.error("新密码与验证密码不匹配");
      }
      else{
        this.request.post("/doctor/changePassword?doctorId="+this.doctorinfo.doctorId+"&password="+ this.oldpassword.toString()+"&newpassword="
            +this.newpassword.toString()).then(res=>{
          if(res.code=="200"){
            this.$message.success("修改成功！请重新登陆！")
            this.$router.push("/")
          } else{
            this.$message.error("原密码错误,修改失败！");
          }

        })
      }
    },
    cancel(){
      this.dialogFormVisible=false
      this.newpassword="";
      this.oldpassword="";
      this.newpasswordag="";
    },
    changepassword(){
      this.dialogFormVisible=true;
      this.newpassword="";
      this.oldpassword="";
      this.newpasswordag="";
    }
  }

}
</script>

<style scoped>
.doctorinfo .el-input{
  width: 300px;
}
</style>